Importing functionality from Java

Java interfaces and classes can be imported from Java by declaring them inside importJava block:

importJava "java.util.regex.Pattern" where
    data Pattern

importJava "java.util.List" where
    data List a

Java methods, constructors and fields can be similarly imported by giving their type annotations in importJava block:

importJava "java.util.regex.Pattern" where
    @JavaName compile
    compilePattern :: String -> Pattern

    @JavaName matcher
    createMatcher :: Pattern -> String -> <Proc> Matcher

importJava "java.util.regex.Matcher" where
    data Matcher

    @JavaName matches
    matcherMatches :: Matcher -> <Proc> Boolean

matches :: Pattern -> String -> <Proc> Boolean
matches pattern text = do
    matcherMatches (createMatcher pattern text)

Another example:

importJava "java.util.ArrayList" where
    @JavaName "<init>"
    createArrayList :: () -> <Proc> List a

    @JavaName "<init>"
    createArrayListWithCapacity :: Integer -> <Proc> List a

    @JavaName size
    sizeList :: List a -> <Proc> Integer

    @JavaName get
    getList :: List a -> Integer -> <Proc> a

    @JavaName set
    setList :: List a -> Integer -> a -> <Proc> ()

    @JavaName add
    addList :: List a -> a -> <Proc> Boolean

Java constructor is referred with "<init>". If Java method name and SCL name matches the annotation @JavaName can be left out. Java import mechanism tries to be quite flexible. It provides some arguments based on the effects the function has. It also ignores the return value of the Java method if the return type is () in SCL.

Static methods are matched by their SCL type signature alone — no extra argument for the class. Instance methods receive the class instance as their first argument in the SCL type signature.

A major functionality currently still missing is the ability to create new implementations of existing Java interfaces in SCL code or extend an existing class. This can be worked around currently by writing new implementations in Java.

Defining type classes

Type classes define a set of operations that a type must support. Declare a class with the class keyword, and provide instances with instance:

class Container f where
    empty  :: f a
    insert :: a -> f a -> f a
    toList :: f a -> [a]

instance Container [] where
    empty  = []
    insert x xs = [x] + xs
    toList = id

Type class constraints in signatures use the ClassName typeVar => syntax. Several constraints are written one after another, each followed by its own =>; the Haskell-style tuple (Container f, Show a) => is rejected with Invalid constraint:

printAll :: Container f => Show a => f a -> <Proc> ()
printAll c = iter (print . show) (toList c)

Classes form a hierarchy: a class may declare superclasses that any instance must also satisfy. Prelude declares, for example:

class (Ring a, Ord a) => OrderedRing a where
    abs :: a -> a
    abs x = if x < zero then neg x else x

The definition of abs inside the class is a default, used by the instances that do not define abs themselves. Unlike Haskell, SCL has no Eq class: == and != accept values of any type.

In class, instance and deriving instance declarations the constraints are written the other way round from type signatures: always in parentheses, separated by commas, as in instance (Show a, Show b) => Show (a, b). Without the parentheses, or chained with =>, they are a syntax error.

Defining effects

New effect types are declared with the effect keyword:

effect MyEffect

Functions that use this effect declare it in their return type:

doSomething :: Integer -> <MyEffect> ()

Effects are used to track which side-channel capabilities a function uses, enabling safe composition and static verification that effects are handled at the correct layer.

Binary operator precedence

Custom binary operators can be given explicit precedence and associativity using infix, infixl, or infixr declarations:

infixl 6 (+++)
infixr 5 (<+>)

(+++) :: Integer -> Integer -> Integer
a +++ b = a + b

The operator must be written in parentheses in the declaration, as when it is used as a function; infixl 6 +++ is a syntax error. Several operators can share one declaration, separated by commas, and ordinary functions can be given a fixity too: infixl 7 (*), (/), div, mod.

  • infixl — left-associative (most arithmetic operators, &)
  • infixr — right-associative ($, ., &&, ||, >>=)
  • infix — non-associative (comparison operators; chaining is an error)

The numeric argument is the precedence level (higher binds tighter). Function application binds tighter than any operator. The levels of the standard operators, declared at the top of the Prelude module (== in Builtin), are:

Level Operators
10 ! (right)
9 . (right)
8 ^ (right)
7 *, /, div, mod (left)
6 +, - (left)
5 \\, <<, <+ (left)
4 ==, !=, <, <=, >=, > (non-associative)
3 &&, &<& (right)
2 ||, orElse, orElseM, morelse (right)
1 >>=, >>, :=, >=>, $ (right); &, catch (left)

$ and & share level 1 but associate in opposite directions, so do not mix them in one expression without parentheses.

Restricted imports

An import statement can expose only a subset of a module's exports:

import "Prelude" (map, filter, foldl)

Or hide specific names while importing everything else:

import "Prelude" hiding (sort)

This is useful for avoiding name clashes when two modules export the same identifier.

Documentation strings

A triple-quoted string literal placed immediately before a definition serves as its documentation comment:

"""
Computes the square root of the sum of squares of the components.
"""
magnitude :: (Double, Double, Double) -> Double
magnitude (x, y, z) = sqrt (x*x + y*y + z*z)

Documentation strings are available to tooling (hover text in the editor, generated docs).

Private definitions

Definitions can be marked private to prevent them from being exported from the module:

private
    helperFunction :: Integer -> Integer
    helperFunction n = n * 2 + 1

Private definitions are invisible to importers; they can only be used within the same module.

Relational sublanguage

SCL includes a relational sublanguage for declarative data queries and constraint enforcement, built on the same infrastructure as the Simantics graph database rules.

Key constructs:

  • select — a query expression: select ?x where { ... } returns a list of all solutions, select first the first solution as a Maybe, and select distinct the solutions without repeated results.
  • :- — defines a relation: Name ?x ?y :- <query>. Inside a do block the body is an ordinary query. At module level it is divided into sections, each headed by an annotation: a binding pattern such as @bbf, with one letter per argument (b bound, f free), heads a query that computes the free arguments from the bound ones, and @enforce heads what enforce does for the relation.
  • enforce — enforce { ... } makes a query true by adding the facts it describes, where the relations support that: it claims graph statements, runs Execute actions, or uses a relation's @enforce section.
  • rule / mapping relation / transformation — rule Name where ... declares a mapping rule, whose facts are grouped into @when, @from, @to and @where sections, over relations declared with mapping relation. An expression transformation Name where <seed facts> runs the mapping rules of its module and of the modules it imports forward from the seed facts when it is evaluated, as a <Proc> action. Nothing runs automatically, and the name is only a label.

This sublanguage is primarily used for ontology-level programming. It is described in 4.03 Relational sublanguage.

SCL also embeds a forward-chaining CHR (Constraint Handling Rules) engine, which reuses the select keyword but is otherwise a separate mechanism with its own rules, constraints and rule stores. Its rules are written when <head> then <body> or <head> => <body>; the when keyword belongs to CHR only, the @when section of a mapping rule is an annotation. CHR is documented in 4.02 Constraint Handling Rules. Note that a module cannot use both: enabling the chr feature turns rule and transformation into ordinary identifiers, so the mapping rules and transformations described above are not available.